<?php
include_once('config/conn.php');

$LoginAdmin = auth();

if (!empty($_GET['logout'])) {
    if ($_GET['logout'] == 1) {
        session_unset();
        ShowMsg('退出成功', 'login.php');
    }
}

$courseList = "SELECT * FROM course";

$course = find($courseList);

if (!empty($_GET['search'])) {
    $search = $_GET['search'];
    $sql = "SELECT * FROM `course` WHERE `name` LIKE '%$search%' OR `teacher` LIKE '%$search%' OR `score` LIKE '%$search%' OR `course_time` LIKE '%$search%' OR `course_type` LIKE '%$search%' OR `id` LIKE '$search%'";
    $course = search($sql);
    if (!empty($course)) {
        if (is_array($course[0])) {
            $course = $course;
        } if (!is_array($course[0])) {
            $course = [$course];
        }
    }
}

if (!empty($_POST['name'])) {
    $name = $_POST['name'];

    $data = [
        'name' => $name,
        'score' => $_POST['score'],
        'teacher' => $_POST['teacher'],
        'course_time' => $_POST['course_time'],
        'course_type' => $_POST['course_type'],
        'start_time' => $_POST['start_time'],
        'end_time' => $_POST['end_time']
    ];
    $res = add('course', $data);
    if ($res) {
        ShowMsg('新增课程信息成功', 'course.php');
    } else {
        ShowMsg('新增课程信息失败');
    }
}

if (!empty($_POST['courseName'])) {
    $id = $_POST['courseID'];
    $name = $_POST['courseName'];
    $data = [
        'name' => $name,
        'score' => $_POST['scoreInt'],
        'teacher' => $_POST['teacherString'],
        'course_time' => $_POST['course_timeInt'],
        'course_type' => $_POST['course_typeString'],
        'start_time' => $_POST['start_timeDate'],
        'end_time' => $_POST['end_timeDate']
    ];
    // $result = update('course', $data, "`id` = '$id'");
    // echo $result;
    // if ($result) {
    //     ShowMsg('更新成功', 'course.php');
    // } else {
    //     ShowMsg('更新失败', 'course.php');
    // }
}

if (!empty($_POST['deleteID'])) {
    $id = $_POST['deleteID'];
    $Sql = "SELECT * FROM `course` WHERE `id` = '$id'";
    $admin = find($Sql);

    if (!$admin) {
        error('删除的课程信息不存在');
    }
    $result = delete('course', "`id` = '$id'");

    if ($result) {
        ShowMsg("删除课程信息成功", 'course.php');
    } else {
        ShowMsg("删除课程信息失败", 'course.php');
    }
}

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>课程信息管理</title>
    <?php include_once('basic/meta.php') ?>
    <style>
        th,
        td {
            height: 30px;
            line-height: 30px;
            font-family: 华文中宋;
            font-weight: 400;
        }

        th {
            font-weight: 600;
        }

        .datepicker-days {
            padding: 30px;
        }

        .table-condensed {
            text-align: center;
        }
    </style>
</head>

<body>
    <?php include_once('basic/header.php'); ?>
    <?php include_once('basic/nav.php'); ?>
    <div class="main">
        <div class="card">
            <div class="card-body" style="font-weight: 600;">
                <div style="display: flex; justify-content: space-between">
                    <p>课程信息管理</p>
                    <div>
                        <form action="" style="display: flex; align-items: center" method="get">
                            <input
                                class="input is-primary"
                                type="text"
                                name="search"
                                style="width: 300px;"
                                placeholder="请输入检索信息" />
                            <button class="button is-link" type="submit"><i class="iconfont icon-Search"></i></button>
                        </form>
                    </div>
                    <button style="height: 40px; font-family: 华文中宋;"
                        type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#collegeAdd">
                        创建课程
                    </button>
                    <!-- 模态框 -->
                    <div class="modal fade" id="collegeAdd" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true" style="position: absolute; z-index: 9999; left: -75px; top:-20px;">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <form action="" method="post">
                                    <!-- 模态框头部 -->
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="myModalLabel" style="font-family: 华文中宋;">添加课程信息</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                    </div>

                                    <!-- 模态框主体 -->
                                    <div class="modal-body">
                                        <input
                                            class="input is-primary"
                                            type="text"
                                            name="name"
                                            style="height: 40px; margin-top: 20px"
                                            placeholder="Course Name input" />
                                        <input
                                            class="input is-primary"
                                            type="number"
                                            name="score"
                                            style="height: 40px; margin-top: 20px"
                                            placeholder="Course Score input" />
                                        <input class="input is-primary"
                                            type="text"
                                            name="teacher"
                                            placeholder="Course Teacher input"
                                            style="height: 40px; margin-top: 20px" />
                                        <input
                                            class="input is-primary"
                                            type="number"
                                            name="course_time"
                                            style="height: 40px; margin-top: 20px"
                                            placeholder="Course Time input" />
                                        <select class="form-select form-control input is-primary" name="course_type" style="height: 40px; margin-top: 20px">
                                            <option value="必修">必修</option>
                                            <option value="选修">选修</option>
                                        </select>
                                        <div class="mb-3 ">
                                            <div class="input-group date" id="start_datepicker">
                                                <input type="text" class="input is-primary" style="height: 40px; margin-top: 20px" placeholder="请选择课程开始日期" name="start_time">
                                                <span class="input-group-append" style="width: 350px; ">
                                                </span>
                                            </div>
                                        </div>
                                        <div class="mb-3 ">
                                            <div class="input-group date" id="end_datepicker">
                                                <input type="text" class="input is-primary" style="height: 40px; margin-top: 20px" placeholder="请选择课程结束日期" name="end_time">
                                                <span class="input-group-append" style="width: 350px; ">
                                                </span>
                                            </div>
                                        </div>
                                    </div>
                                    <!-- 模态框底部 -->
                                    <div class="modal-footer" style="font-family: 华文中宋;">
                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                                        <button type="submit" class="btn btn-secondary" data-bs-dismiss="modal">提交</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="overflow-auto" style="height: 515px; margin-top:20px">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th scope="col">ID</th>
                                <th scope="col">课程名称</th>
                                <th scope="col">课程学分</th>
                                <th scope="col">任课老师</th>
                                <th scope="col">课程学时</th>
                                <th scope="col">课程类型</th>
                                <th scope="col">开始时间</th>
                                <th scope="col">结束时间</th>
                                <th scope="col">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($course as $key => $item) {
                            ?>
                                <tr>
                                    <?php foreach ($item as $k => $v) {  ?>
                                        <td><?php echo $v; ?></td>
                                    <?php } ?>
                                    <td style="width: 125px;">
                                        <button type="button" class="btn btn-primary" style="height: 35px; width: 35px;" data-bs-toggle="modal" data-bs-target="#majorEdit" data-table-id="<?php echo $item[0]; ?>">
                                            <i class="iconfont icon-doc-edit"></i>
                                        </button>
                                        <button type="button" class="btn btn-primary" style="height: 35px; width: 35px" data-bs-toggle="modal" data-bs-target="#majorDelete" data-table-id="<?php echo $item[0]; ?>">
                                            <i class="iconfont icon-delete"></i>
                                        </button>
                                    </td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                    <!-- 模态框 -->
                    <div class="modal fade" id="majorEdit" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true" style="position: absolute; z-index: 9999; left: -75px; top:-100px; height: 700px">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <form action="" method="post">
                                    <!-- 模态框头部 -->
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="myModalLabel" style="font-family: 华文中宋;">修改课程信息</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                    </div>

                                    <!-- 模态框主体 -->
                                    <div class="modal-body">
                                        <input type="text" name="courseID" class="input is-primary" id="courseID" style="height: 40px;" disabled/>
                                        <input
                                            class="input is-primary"
                                            type="text"
                                            name="courseName"
                                            style="height: 40px; margin-top: 20px"
                                            placeholder="Course Name input" />
                                        <input
                                            class="input is-primary"
                                            type="number"
                                            name="scoreInt"
                                            style="height: 40px; margin-top: 20px"
                                            placeholder="Course Score input" />
                                        <input class="input is-primary"
                                            type="text"
                                            name="teacherString"
                                            placeholder="Course Teacher input"
                                            style="height: 40px; margin-top: 20px" />
                                        <input
                                            class="input is-primary"
                                            type="number"
                                            name="course_timeInt"
                                            style="height: 40px; margin-top: 20px"
                                            placeholder="Course Time input" />
                                        <select class="form-select form-control input is-primary" name="course_typeString" style="height: 40px; margin-top: 20px">
                                            <option value="必修">必修</option>
                                            <option value="选修">选修</option>
                                        </select>
                                        <div class="mb-3 ">
                                            <div class="input-group date" id="start_datepicker_update">
                                                <input type="text" class="input is-primary" style="height: 40px; margin-top: 20px" placeholder="请选择课程开始日期" name="start_timeDate">
                                                <span class="input-group-append" style="width: 350px; ">
                                                </span>
                                            </div>
                                        </div>
                                        <div class="mb-3 ">
                                            <div class="input-group date" id="end_datepicker_update">
                                                <input type="text" class="input is-primary" style="height: 40px; margin-top: 20px" placeholder="请选择课程结束日期" name="end_timeDate">
                                                <span class="input-group-append" style="width: 350px; ">
                                                </span>
                                            </div>
                                        </div>
                                    </div>
                                    <!-- 模态框底部 -->
                                    <div class="modal-footer" style="font-family: 华文中宋;">
                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                                        <button type="submit" class="btn btn-secondary" data-bs-dismiss="modal" onclick="updateCourse()">提交</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="modal fade" id="majorDelete" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true" style="position: absolute; z-index: 9999;">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <form action="" method="post">
                                    <!-- 模态框头部 -->
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="myModalLabel" style="font-family: 华文中宋;">是否确认删除该课程信息</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                    </div>
                                    <!-- 模态框主体 -->
                                    <div class="modal-body">
                                        <input type="text" name="deleteID" value="" class="input is-primary" id="deleteID" style="height: 40px;" disabled />
                                    </div>
                                    <!-- 模态框底部 -->
                                    <div class="modal-footer" style="font-family: 华文中宋;">
                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                                        <button type="submit" class="btn btn-secondary" data-bs-dismiss="modal" onclick="deleteCollege()">提交</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
    </div>
    <script>
        // Get the modal
        var modal = document.getElementById('majorEdit');
        var deleteModal = document.getElementById('majorDelete');
        var btns = document.querySelectorAll('button[data-bs-target="#majorEdit"]');

        btns.forEach(function(btn) {
            btn.addEventListener('click', function() {
                var tableId = this.getAttribute('data-table-id');
                document.querySelector('#courseID').value = tableId;
            });
        });

        var btnDelete = document.querySelectorAll('button[data-bs-target="#majorDelete"]');
        btnDelete.forEach(function(btn) {
            btn.addEventListener('click', function() {
                var deleteID = this.getAttribute('data-table-id');
                document.querySelector('input[name="deleteID"]').value = deleteID;
            });
        });

        // 更新数据事件
        function updateCourse() {
            var courseName = document.querySelector('input[name="courseName"]').value;
            var score = document.querySelector('input[name="scoreInt"]').value;
            var time = document.querySelector('input[name="course_timeInt"]').value;
            var type = document.querySelector('select[name="course_typeString"]').value;
            var start = document.querySelector('input[name="start_timeDate"]').value;
            var end = document.querySelector('input[name="end_timeDate"]').value;
            console.log(courseName);
            if (score == '' || courseName == '' || time == '' || type == '' || start == '' || end == '') {
                alert('请填写完整信息');
                document.querySelector('input[name="courseName"]').value = '';
            }else {
                document.querySelector('#courseID').disabled = false;
            }
        }

        // 删除数据事件
        function deleteCollege() {
            document.querySelector('#deleteID').disabled = false;
        }

        $(function() {
            $('#start_datepicker').datepicker({
                language: 'zh-CN',
                autoclose: true,
                todayHighlight: true,
                format: 'yyyy-mm-dd',
                clearBtn: true
            });
            $('#end_datepicker').datepicker({
                language: 'zh-CN',
                autoclose: true,
                todayHighlight: true,
                format: 'yyyy-mm-dd',
                clearBtn: true
            });
            $('#start_datepicker_update').datepicker({
                language: 'zh-CN',
                autoclose: true,
                todayHighlight: true,
                format: 'yyyy-mm-dd',
                clearBtn: true
            });
            $('#end_datepicker_update').datepicker({
                language: 'zh-CN',
                autoclose: true,
                todayHighlight: true,
                format: 'yyyy-mm-dd',
                clearBtn: true
            });
        });
    </script>
</body>

</html>